Google Apps Script
Google Apps Script
表單資料格式
例如0123會被轉換成數值,變成123
須事先點選表單左上角格全選後,將格式設為「純文字(Plain text)」
最大可儲存量為1000萬格資料
最大行數為18278行
一次可新增40000列
字串上限長度為50000字元
本地開發
登入
$ npx @google/clasp login
基於clasp,再加入TypeScript、ESLint、測試環境、環境切換等功能
以Apache License 2.0授權釋出
安裝
$ npx @google/aside init
$ npm run deploy
$ npm run deploy:prod
$ npm run lint
測試
$ npm run test
需啟用Google Apps Script API
ViteSingleFile
$ npm install vite-plugin-singlefile --save-dev
$ npm i @types/google-apps-script --save-dev
code:vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { viteSingleFile} from 'vite-plugin-singlefile';
export default defineConfig({
build: {
outDir: "appsscript/client",
}
});
Script Properties
設定
1. Project Settings中的最底下
2. 使用PropertiesService.getScriptProperties().setProperty
PropertiesService.getScriptProperties().setProperty("Property","Value")
取得
PropertiesService.getScriptProperties().getProperty("Property")
PropertiesService.getScriptProperties().getProperties()
props.property
權限關聯
Container-bound Scripts
和Sheet聯動
Standalone Scripts
從Drive ID取得:SpreadsheetApp.openById(spreadsheetId).getActiveSheet();
從URL取得:SpreadsheetApp.openByUrl(spreadsheetURL).getSheetByName(sheetName);
Protection
可透過網址請求即時取得QR Code圖片
https://chart.googleapis.com/chart?chs=450x450&cht=qr&chl=內容字串
此API已不再維護,隨時都可能無法再使用
基礎方針
一次處理整個試算表的讀寫
例子
code:javascript
for (let i=2; i<=lastRow; i++) {
const status = sheet.getRange(i, 1).getValue();
if (status === 'finished') {
sheet.getRange(i, 2).setValue('f');
}
}
改為一次全部進行修改
code:javascript
const taskNames = sheet.getRange(2, 1, lastRow-1, 1).getValues();
const results = taskNames.map(row => row0 === 'finished' ? 'f' : ''); sheet.getRange(2, 2, results.length, 1).setValues(results);
兩者的處理時間會差到超過三分鐘
同步送出外部請求
UrlFetchApp.fetchAll
應用方針
活用快取
假性非同步處理
定期執行